package com.design.proxy.dbroute.proxy;

import com.design.proxy.dbroute.IOrderService;
import com.design.proxy.dbroute.Order;
import com.design.proxy.dbroute.db.DynamicDataSourceEntity;

import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 动态代理切换数据源
 * @Author:         cong zhi
 * @CreateDate:     2021/3/23 19:27
 * @UpdateUser:     cong zhi
 * @UpdateDate:     2021/3/23 19:27
 * @UpdateRemark:   修改内容
 * @Version:        1.0
 */
public class OrderServiceStaticProxy implements IOrderService {

    private SimpleDateFormat yearFomat = new SimpleDateFormat("yyyy");

    private IOrderService orderService;

    public OrderServiceStaticProxy(IOrderService orderService) {
        this.orderService = orderService;
    }

    @Override
    public int createOrder(Order order) {

        Long time = order.getCreateTime();
        Integer dbRouter = Integer.valueOf(yearFomat.format(new Date(time)));
        System.out.println("静态代理类自动分配到【DB_" + dbRouter + "】数据源处理数据");
        DynamicDataSourceEntity.set(dbRouter);
        this.orderService.createOrder(order);
        DynamicDataSourceEntity.restore();
        return 0;
    }


}
